import { seriseList, delSeries } from '@/services/ant-design-pro/source';
import type { ActionType, ProColumns, ProDescriptionsItemProps } from '@ant-design/pro-components';
import {
  FooterToolbar,
  PageContainer,
  ProDescriptions,
  ProTable,
} from '@ant-design/pro-components';
import { FormattedMessage, useIntl, useRequest } from '@umijs/max';
import { Button, Drawer, Input, message } from 'antd';
import React, { useCallback, useRef, useState } from 'react';
import CreateForm from './components/CreateForm';
import UpdateForm from './components/UpdateForm';
import { history, useLocation } from '@umijs/max';
import dayjs from 'dayjs';

const TableList: React.FC = () => {
  const actionRef = useRef<ActionType>();

  const [showDetail, setShowDetail] = useState<boolean>(false);
  const [currentRow, setCurrentRow] = useState<API.RuleListItem>();
  const [selectedRowsState, setSelectedRows] = useState<API.RuleListItem[]>([]);
  const param = localStorage.getItem('idArray');
  const paramsData = JSON.parse(param);
  // const paramsData:any = useLocation().state;
  /**
   * @en-US International configuration
   * @zh-CN 国际化配置
   * */
  const intl = useIntl();

  const [messageApi, contextHolder] = message.useMessage();

  const handleNavTo = (record:any) => {
    const idArray = {booksId: record.id, seriseName: record.name, typeId: paramsData.typeId, typeName: paramsData.typeName};
    localStorage.setItem('idArray', JSON.stringify(idArray));
    history.push('/list/category-list/classify-list/collection-list', idArray);
  };
  const handleRemove =  async(data:any) => {
    const res = await delSeries({id: data.id});
    if(res.code === 0){
      messageApi.success('删除成功')
      actionRef.current?.reload()
    }else{
      messageApi.error('删除失败')
    }
  }

  const columns: ProColumns<API.RuleListItem>[] = [
    {
      title:'ID',
      dataIndex: 'id',
      hideInSearch: true,
    },
    {
      title: '编码',
      dataIndex: 'code',
      valueType: 'code',
      hideInSearch: true,
    },
    {
      title: '名称',
      dataIndex: 'name',
      valueType: 'textarea',
    },
    {
      title: '简介',
      dataIndex: 'description',
      valueType: 'textarea',
      hideInSearch: true,
    },
    {
      title: '资源数',
      dataIndex: 'page',
      valueType: 'textarea',
      hideInSearch: true,
    },
    {
      title: '状态',
      dataIndex: 'status',
      hideInSearch: true,
      valueEnum: {
        0: {
          text: (
           '关闭'
          ),
          status: 'Default',
        },
        1: {
          text: (
            '开启'
          ),
          status: 'Success',
        },
      },
    },
    {
      title: '创建时间',
      dataIndex: 'cTime',
      hideInForm: true,
      hideInSearch: true,
      renderText: (val: string) => val === null ? '' : `${dayjs(val).format('YYYY-MM-DD HH:mm:ss')}`,
    },
    {
      title: '修改时间',
      dataIndex: 'mTime',
      hideInSearch: true,
      hideInForm: true,
      renderText: (val: string) => val === null ? '' : `${dayjs(val).format('YYYY-MM-DD HH:mm:ss')}`,
    },
    {
      title: <FormattedMessage id="pages.searchTable.titleOption" defaultMessage="Operating" />,
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => [
        <Button type="link" onClick={()=>handleNavTo(record)}>资源</Button>,
        <UpdateForm
          reload={actionRef.current?.reload}
          key="config"
          onOk={actionRef.current?.reload}
          values={record}
        />,
        <Button type="link" onClick={()=>handleRemove(record)} danger>删除</Button>,
      ],
    },
  ];

  return (
    <PageContainer title={paramsData.typeName}>
      {contextHolder}
      <ProTable<API.RuleListItem, API.PageParams>
        headerTitle={intl.formatMessage({
          id: 'pages.searchTable.title',
          defaultMessage: 'Enquiry form',
        })}
        actionRef={actionRef}
        rowKey="key"
        toolBarRender={() => [<CreateForm key="create" reload={actionRef.current?.reload}  id={paramsData.typeId}/>]}
        request={async (params:{
          pageSize: number;
          current: number;
          name: string;
        })=>{
          const msg = await seriseList({
            typeId: paramsData.typeId,
            name: params.name ?? '',
            pageNo: params.current -1,
            pageSize: params.pageSize,
          })
          return {
            data:msg.obj.list,
            success:msg.obj.total,
          }
        }}
        columns={columns}
      />
    </PageContainer>
  );
};

export default TableList;
